package com.topway.dsmflex.dao.servers;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import org.jdom.JDOMException;

import com.topway.dsmflex.dao.BaseDao;
import com.topway.dsmflex.model.servers.DataServerBean;
import com.topway.dsmflex.util.XmlEditUtil;

public class ServersMonitorDao extends BaseDao {
	
	private static ThreadLocal<ServersMonitorDao> serversMonitorDao= new ThreadLocal<ServersMonitorDao>();
	
	public ServersMonitorDao(){
		try {
			getConnection(XmlEditUtil.getDataSourceByName("SERVERLOG"));
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public synchronized static ServersMonitorDao getServersMonitorDao()
	{
		if(null==serversMonitorDao.get())
		{
			serversMonitorDao.set(new ServersMonitorDao());
		}
		return serversMonitorDao.get();
	}
	

	public int addServersException(DataServerBean dt )
	{
		
		String sqlI = "insert into ServerLog (id,name,use,state,start_date) values('"+UUID.randomUUID().toString()+"','"+dt.getName()+"','"+dt.getRemark()+"',0,sysdate)";
		String sqlQ = "select name from ServerLog where  end_date is  null and state=0";
		try {
			ResultSet rs = conn.createStatement().executeQuery(sqlQ);
			if(!rs.next())
				this.conn.createStatement().execute(sqlI);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return 1;
	}
	
	public int updServersException(DataServerBean dt )
	{
		
		String sql = "update ServerLog set state=1,end_date=sysdate where end_date is null and state=0";
		try {
			this.conn.createStatement().execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	
//	-- Create table
//	create table SERVERLOG
//	(
//	  ID         VARCHAR2(38) not null,
//	  NAME       VARCHAR2(15),
//	  USE        VARCHAR2(50),
//	  STATE      NUMBER(1),
//	  START_DATE DATE,
//	  END_DATE   DATE,
//	  REMARK     VARCHAR2(50)
//	)
//	tablespace DSMFLEX
//	  pctfree 10
//	  initrans 1
//	  maxtrans 255
//	  storage
//	  (
//	    initial 64
//	    minextents 1
//	    maxextents unlimited
//	  );
//	-- Create/Recreate primary, unique and foreign key constraints 
//	alter table SERVERLOG
//	  add constraint PK_SERVERLOG primary key (ID)
//	  using index 
//	  tablespace DSMFLEX
//	  pctfree 10
//	  initrans 2
//	  maxtrans 255
//	  storage
//	  (
//	    initial 64K
//	    minextents 1
//	    maxextents unlimited
//	  );

}
